package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

/**
 * @Author by 孙有泽
 * @Date 2025/3/3 14:08
 * @Description 菜品管理
 */

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(value = "菜品管理",tags = "菜品管理")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /*
     * @author 孙有泽
     * @date 2025/3/3 17:16
     * @description 新增菜品
     */
    @ApiOperation(value = "新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDto){
        log.info("新增菜品：{}",dishDto);
        dishService.save(dishDto);
        return Result.success();
    }
    /*
     * @author 孙有泽
     * @date 2025/3/3 17:29
     * @description 菜品列表
     */
    @ApiOperation(value = "菜品列表")
    @GetMapping("/page")
    public Result<PageResult> list(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品列表查询：{}",dishPageQueryDTO);
        PageResult pageResult =dishService.list(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    /*
     * @author 孙有泽
     * @date 2025/3/3 18:04
     * @description 菜品批量删除
     */
    @ApiOperation(value = "菜品批量删除")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除：{}",ids);
        dishService.delete(ids);
        /*清空缓存*/
        Set dish = redisTemplate.keys("dish_*");
        redisTemplate.delete(dish);
        return Result.success();
    }
    /*
     * @author 孙有泽
     * @date 2025/3/3 20:46
     * @description 菜品的起售和停售
     */
    @ApiOperation(value = "菜品起售和停售")
    @PostMapping("/status/{status}")
    public Result updateStatus(@RequestParam Long id,@PathVariable Integer status){
        log.info("菜品起售和停售：{}",id);
        dishService.updateStatus(status,id);
        /*清空缓存*/
        Set dish = redisTemplate.keys("dish_*");
        redisTemplate.delete(dish);
        return Result.success();
    }

    /*
     * @author 孙有泽
     * @date 2025/3/3 21:13
     * @description 根据id查询菜品 更新的回显
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /*
     * @author 孙有泽
     * @date 2025/3/3 22:05
     * @description 根据分类查询菜品
     */
    @ApiOperation("根据分类查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> list(@RequestParam Long categoryId){
        log.info("根据分类查询菜品：{}",categoryId);
        List<DishVO> dishVOS = dishService.selectDishByCategoryId(categoryId);
        return Result.success(dishVOS);
    }
    /*
     * @author 孙有泽
     * @date 2025/3/4 13:47
     * @description 菜品的修改
     */
    @ApiOperation("菜品修改")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("菜品修改：{}",dishDTO);
        dishService.update(dishDTO);
        /*清空缓存*/
        Set dish = redisTemplate.keys("dish_*");
        redisTemplate.delete(dish);
        return Result.success();

    }






}
